package com.example.nbshangpping.buy;

import android.content.Intent;
import android.os.Bundle;
import android.view.View;
import android.widget.AdapterView;
import android.widget.Button;
import android.widget.ImageView;
import android.widget.ListView;
import android.widget.TextView;

import com.example.nbshangpping.R;
import com.example.nbshangpping.goods.DetailsGoodsActivity;
import com.example.nbshangpping.login.ChangePwdActivity;
import com.example.nbshangpping.login.UserBean;
import com.example.nbshangpping.utils.BaseActivity;
import com.example.nbshangpping.utils.SaveUserUtils;
import com.example.nbshangpping.utils.URLUtils;
import com.google.gson.Gson;

import java.io.Serializable;
import java.util.ArrayList;
import java.util.List;

public class CartGoodsActivity extends BaseActivity {
    ImageView shoppingcariv;
    ListView shoppingcarlv;
    TextView pricetv;
    Button clearbtn;
    List<CartGoodsBean.DataBean> selectdatas;
    List<CartGoodsBean.DataBean> data;
    List<Boolean>statuList;
    private UserBean.DataBean userInfo;
    private CartGoodsAdapter adapter;
    double totalMoney = 0.0;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_cart_goods);
        userInfo= SaveUserUtils.getUserInfo(this);
        initView();
    }

    @Override
    public void onSuccess(String result) {
        super.onSuccess(result);
        CartGoodsBean shoppingBean = new Gson().fromJson(result, CartGoodsBean.class);
        List<CartGoodsBean.DataBean> list = shoppingBean.getData();
        data.addAll(list);
        int datasize=data.size();
        for (int i=0;i<datasize;i++)
        {
            statuList.add(false);
        }
        adapter.notifyDataSetChanged();
    }


    AdapterView.OnItemClickListener listener=new AdapterView.OnItemClickListener() {
        @Override
        public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
            CartGoodsBean.DataBean dataBean = data.get(position);
            Intent intent = new Intent(CartGoodsActivity.this, DetailsGoodsActivity.class);
            intent.putExtra("goods",dataBean);  //将数据传递给下一级页面
            startActivity(intent);
        }
    };
    @Override
    protected void onStart() {
        super.onStart();
        ShowGoods();
    }
    @Override
    protected void onResume() {
        super.onResume();
        totalMoney=0.0;
        pricetv.setText("¥0");
    }
    private void selectGoods() {
        selectdatas=new ArrayList<>();
        for(int i=0;i<data.size();i++){
            if(statuList.get(i)){
                selectdatas.add(data.get(i));
            }
        }
    }
    private void ShowGoods() {
        //数据源
        data = new ArrayList<>();
        //适配器
        statuList=new ArrayList<>();
        adapter = new CartGoodsAdapter(this,data,statuList);
        shoppingcarlv.setAdapter(adapter);
        //联网
        adapter.selectGoodsListener(new CartGoodsAdapter.SelectGoodsListener() {
            @Override
            public void selectGoods(boolean status, int pos) {
                CartGoodsBean.DataBean bean=data.get(pos);
                double totalprice =bean.getTotalprices();
                if (status){
                    totalMoney+=totalprice;
                    pricetv.setText("¥"+totalMoney);
                }else {
                    if (totalMoney>=totalprice){
                        totalMoney-=totalprice;
                        pricetv.setText("¥"+totalMoney);
                    }
                }
            }
        });

        getNetwork(URLUtils.queryShoppingCar_url+userInfo.getId());
    }
    private void initView() {
        shoppingcariv=findViewById(R.id.shoppingcar_iv_back);
        pricetv=findViewById(R.id.shoppingcar_tv2);
        shoppingcarlv=findViewById(R.id.shoppingcar_gv);
        clearbtn=findViewById(R.id.shoppingcar_btn);
        shoppingcariv.setOnClickListener(onClicklistener);
        shoppingcarlv.setOnItemClickListener(listener);
        clearbtn.setOnClickListener(onClicklistener);
    }

    View.OnClickListener onClicklistener=new View.OnClickListener() {
        @Override
        public void onClick(View v) {
            switch (v.getId()){
                case R.id.shoppingcar_iv_back:
                    finish();
                    break;
                case R.id.shoppingcar_btn:
                    selectGoods();
                    Intent intent=new Intent(CartGoodsActivity.this,BuyActivity.class);
                    intent.putExtra("selectdatas", (Serializable) selectdatas);
                    startActivity(intent);
                    break;
            }
        }
    };
}
